package com.easy.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alipay.api.AlipayApiException;
import com.easy.bean.Orderdetails;
import com.easy.bean.Orders;
import com.easy.dao.CartDao;
import com.easy.dao.OrderdetailsDao;
import com.easy.dao.OrdersDao;
import com.easy.service.IOrdersService;
import com.easy.util.Page;
import com.easy.util.PaymentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;

@Service
public class OrdersService implements IOrdersService {
    @Autowired
    OrdersDao ordersDao;
    @Autowired
    OrderdetailsDao orderdetailsDao;
    @Autowired
    CartDao cartDao;

    @Transactional
    @Override
    public int addOrders(Orders orders) {
        ordersDao.addOrders(orders);
        int orderid=orders.getId();
        int userid=orders.getUserid();
        orderdetailsDao.insertOrderdetails(userid,orderid);
        cartDao.deletechecked(userid);
        return orderid;
    }

//    //准备支付，生成二维码
//    String url=null;
//    url=PaymentUtil.payment(order);
//    BUfferedImage img= QrCodeUtil.generate(url, 300, 300);
//    //将图片转成Base64位编码
//    String qrcode=ImgUtil.toBase64(img,"png");
//    return qrcode;

    @Override
    public int editOrders(Orders orders) {
        return ordersDao.editOrders(orders);
    }

    @Override
    public int deleteOrders(int id) {
        return ordersDao.deleteOrders(id);
    }

    @Override
    public List<Orders> selectOrders(Page page, String text) {
        return ordersDao.selectOrders(page,text);
    }


    @Override
    public Orders getOrdersById(int id) {
        return ordersDao.getOrdersById(id);
    }

    @Override
    public int OrdersCount(String text) {
        return ordersDao.OrdersCount(text);
    }

    @Override
    public List<Orders> getOrderdetailsByUserid(int userid) {
        return ordersDao.getOrderdetailsByUserid(userid);
    }

    @Override
    public Orders getOrderByOrderId(int orderid) {
        return ordersDao.getOrderByOrderId(orderid);
    }

    @Override
    public String payment(int orderid) {
        Orders order=ordersDao.getOrderByOrderId(orderid);
        System.out.println(order.getStatus());
        if(order==null){
            throw new RuntimeException("订单不存在！");
        }
        if(order.getStatus().equals("已失效")){
            throw new RuntimeException("订单已失效！");
        }
        if(!order.getStatus().equals("未支付")){
            throw new RuntimeException("订单已支付！");
        }
        //准备支付，生成二维码
        String url=null;
        try {
            url=PaymentUtil.payment(order);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        BufferedImage img= QrCodeUtil.generate(url, 300, 300);
        //将图片转成Base64位编码
        String qrcode=ImgUtil.toBase64(img,"png");
        return qrcode;
    }

    @Override
    public int cancleOrder() {
        return ordersDao.cancleOrder();
    }

    @Override
    public Orders getOrderByCode(String code) {
        return ordersDao.getOrderByCode(code);
    }
}
